/**
 * 矩阵键盘密码锁: https://www.bilibili.com/video/BV1Mb411e7re?p=16
 * 判断4位密码
 */
#include "Link/LCD1602_Utils.h"
#include "Link/Matrix_Keyboard.h"

void main_matrix_keyboard_password() {
    unsigned char pressedKey, lengh = 0;
    int passWard = 0;
    LCD1602_Init(1);
    LCD_ShowString(1, 1, "Password:");
    while (1) {
        pressedKey = MatrixKeyDown(1);
        //前10个按键分别对应: 1~9, 0
        if (pressedKey > 0 && pressedKey <= 10) {
            if (lengh < 4) {            //只能输入4位密码
                passWard *= 10;         //密码向前移1位
                passWard += pressedKey; //加上输入的个位数
                lengh++;
                LCD_ShowNum(2, 1, passWard, lengh);
            }
        } else if (pressedKey == 11) {  //确认
            if (passWard == 2345) {
                LCD_ShowString(1, 14, "Ok ");
            } else {
                LCD_ShowString(1, 14, "Err");
            }
            passWard = 0;               //密码清0, 可重新输入
            LCD_ShowNum(2, 1, passWard, lengh);//0000
            lengh = 0;
        } else if (pressedKey == 12) {  //清空
            passWard = 0;               //密码清0, 可重新输入
            LCD_ShowNum(2, 1, passWard, lengh);//0000
            lengh = 0;
        }
        
        //第1列有问题, 用空格覆盖掉
        if (pressedKey == 1 || pressedKey == 5 || pressedKey == 9 || pressedKey == 13) {
            LCD_ShowString(2, lengh + 1, "              ");
        }
    }
}